package Double_No_Cyclic_LikedeList;

/**
 * Created with IntelliJ IDEA.
 * Description:实现无头双链表的各种操作
 * User: 阿宾
 * Date: 2024-11-11
 * Time: 15:55
 */
public class MyLinkedList {
    static class ListNode{
        int val;
        private ListNode prev;
        private ListNode next;

        //构造方法
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//双链表的头结点
    public ListNode last;//双链表的尾结点


    /**
     * 求双链表的长度
     * @return
     */
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    /**
     * 遍历双链表(从前向后遍历)
     */
    public void display(){
        if(head == null){
            System.out.println("链表为空");
            return;
        }
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 查找是否包含关键字key是否在单链表当中
     * @param key
     * @return
     */
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 头插法
     * @param data
     */
    public void addFirst(int data){
        /*//1.创建新的链表节点
        ListNode newNode = new ListNode(data);

        //2.先判断是否为空，为空则单独处理，然后插入链表头部
        if(head == null){
            head = newNode;
            last = newNode;
        }else {
            newNode.next = head;
            head.prev = newNode;
            //3.更新链表头节点
            head = newNode;
        }*/
        //1.创建新的链表节点
        ListNode newNode = new ListNode(data);
        ListNode cur = head;
        //2.更新链表头结点
        head = newNode;
        //3.判断当前插入节点是否是第一个结点
        if(cur == null){
            last = newNode;
        }else {
            cur.prev = newNode;
            newNode.next = cur;
        }


    }

    /**
     * 尾插法
     * @param data
     */
    public void addLast(int data){
        //1.创建新的链表节点
        ListNode newNode = new ListNode(data);
        //2.插入链表尾部
        if(last == null){
            head = newNode;
            last = newNode;
        }else {
            last.next = newNode;
            newNode.prev = last;
            //3.更新链表尾节点
            last = newNode;
        }
    }

    /**
     * 任意位置插入,第一个数据节点为0号下标
     * @param index
     * @param data
     */
    public void addIndex(int index,int data){
        //1.判断插入位置是否合法
        if(index < 0 || index > size()){
            System.out.println("插入位置不合法");
            return;
        }
        //2.判断插入位置是第一个还是最后一个
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        //3.插入其他位置
        //3.1、找到下标为index-1的位置
        ListNode cur = head;
        while(index-1 > 0){
            cur = cur.next;
            index--;
        }
        //cur所指的就是index位置的前一个节点
        //3.2、插入新节点
        ListNode newNode = new ListNode(data);
        newNode.next = cur.next;
        cur.next.prev = newNode;
        newNode.prev = cur;
        cur.next = newNode;

    }


    /**
     * 删除第一次出现关键字为key的节点
     * @param key
     */
    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //1.删除头结点
                if(cur == head){
                    head = cur.next;
                    //需要考虑只有一个节点且这个节点就是要删除的节点情况
                    if(head != null){
                        head.prev = null;
                    }else {
                        //即链表为空，此时head已经为空
                        last = null;
                    }

                }else if(cur == last){
                    //2.删除尾结点
                    last = cur.prev;
                    last.next = null;
                }else {
                    //3.删除其他结点
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
                return;
            }
            cur = cur.next;
        }

    }

    /**
     * 删除所有值为key的节点
     * @param key
     */
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //1.删除头结点
                if(cur == head){
                    head = cur.next;
                    //需要考虑只有一个节点且这个节点就是要删除的节点情况
                    if(head != null){
                        head.prev = null;
                    }else {
                        //即链表为空，此时head已经为空
                        last = null;
                    }

                }else if(cur == last){
                    //2.删除尾结点
                    last = cur.prev;
                    last.next = null;
                }else {
                    //3.删除其他结点
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
            }
            cur = cur.next;
        }
    }


    /**
     * 清空链表
     */
    public void clear(){
//        head = last = null;
        ListNode cur = head;
        ListNode pcur;
        while(cur != null){
            pcur = cur.next;
            cur.prev = cur.next = null;
            cur = pcur;
        }
        head = last = null;
    }
}


